package xj.graph2d.tools;

import java.awt.Dimension;
import java.awt.Point;
import java.util.Observer;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.DrawShapeSearch;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.GraphNodeBase;
import xj.graph2d.LineShape;
import xj.graph2d.shapes.RectangularShape;
import xj.graph2d.shapes.RegularPolygonShape;
import xj.graph2d.shapes.ShapeConstants;
import xj.util.gui.DialogLayout;
import xj.util.gui.OptionDialog;
import xj.util.gui.OptionPanel;

// currently not used. 

public class GraphPointTool extends PointTool {

  public GraphPointTool() {
  }

  protected OptionDialog graphNodeDialog = null;

  protected OptionDialog graphEdgeDialog = null;

  protected GraphNodeBase curNode;

  protected GraphEdgeBase curEdge;

  @Override
  public void mouseDragged(Observer observer, Point p, int modifiers) {
    if (mouseButtonDown && selectedShape != null && view != null) {
      displayLocation(p);

      if (selectedShape instanceof LineShape) {

      } else {
	super.mouseDragged(observer, p, modifiers);
      }
    }
  }

  @Override
  public void mouseLButtonDoubleClicked(Observer observer, Point p,
					int modifiers) {
    // System.out.println("GraphPointTool.mouseLButtonDoubleClicked()
    // selectedShape=" + selectedShape);
    start(observer);
    showGraphNodePanel();
  }

  @Override
  public void mouseRButtonPressed(Observer observer, Point p, int modifiers) {
    selectedShape = null;
    // mouseButtonDown = true;
    start(observer);
    if (view != null) {
      // Graphics2D g2 = canvas.getGraphics2D();
      // canvas.setRenderingHints(g2);
      // canvas.setTransform(g2);
      view.hideThumbNail();

      prev = view.physicalToVirtual(p);

      // System.out.println(doc.toString());

      DrawShape prevSelectedShape = doc.getSelectedShape();
      if (prevSelectedShape != null) {
	doc.setSelectedShape(null);
	canvas.repaintShape(prevSelectedShape, true);
      }

      DrawShapeSearch.Result result = doc.search(prev);

      // System.out.println("PointTool.mouseLButtonPressed pos=" +
      // result.pos);

      if (result != null && result.position != DrawShapeConstants.PointOutside) {
	selectedShape = result.shape;
	pos = result.position;

	showGraphNodePanel();
      }
    }

  }

  protected void showGraphNodePanel() {

    if (selectedShape instanceof GraphNodeBase) {
      curNode = (GraphNodeBase) selectedShape;
      if (graphNodeDialog == null) {
	// ResourceManager resourceManager = view.getResourceManager();
	OptionPanel[] options = { new GraphNodePanel("Graph Node"),
	// resourceManager.getResourceString("Document.title")),
	};

	graphNodeDialog = new OptionDialog(null, null, options);
      }
      graphNodeDialog.setOptions();
      graphNodeDialog.showOptionDialog();
    } else if (selectedShape instanceof GraphEdgeBase) {
      curEdge = (GraphEdgeBase) selectedShape;
      if (graphEdgeDialog == null) {
	// ResourceManager resourceManager = view.getResourceManager();
	OptionPanel[] options = { new GraphEdgePanel("Graph Edge"),
	// resourceManager.getResourceString("Document.title")),
	};

	graphEdgeDialog = new OptionDialog(null, null, options);
      }
      graphEdgeDialog.setOptions();
      graphEdgeDialog.showOptionDialog();
    }
  }

  class GraphNodePanel extends OptionPanel {

    public GraphNodePanel(String name) {
      super(name);
      // ResourceManager resourceManager = view.getResourceManager();
      // JLabel authorLabel = new
      // JLabel(resourceManager.getResourceString("Author.text") + " ");
      // authorField = new JTextField();

      JLabel nameLabel = new JLabel("Node name: ");
      nameField = new JTextField();

      orientationLabel = new JLabel("Orientation: ");
      orientationChoice = new JComboBox();
      orientationChoice.addItem("North");
      orientationChoice.addItem("East");
      orientationChoice.addItem("South");
      orientationChoice.addItem("West");

      mainPanel.setLayout(new DialogLayout());
      addRow(nameLabel, nameField);
      addRow(orientationLabel, orientationChoice);
      // setOption();
    }

    @Override
    public void setOption() {
      if (curNode != null) {
	String name = curNode.getName();
	nameField.setText(name);
	if (curNode instanceof RegularPolygonShape) {
	  orientationLabel.setEnabled(true);
	  orientationChoice.setEnabled(true);
	  RegularPolygonShape regNode = (RegularPolygonShape) curNode;
	  orientationChoice.setSelectedIndex(regNode.getDirection()
	      - ShapeConstants.NORTH);
	} else if (curNode instanceof RectangularShape) {
	  orientationLabel.setEnabled(true);
	  orientationChoice.setEnabled(true);
	  RectangularShape rectNode = (RectangularShape) curNode;
	  orientationChoice.setSelectedIndex(rectNode.getDirection()
	      - ShapeConstants.NORTH);
	} else {
	  orientationLabel.setEnabled(false);
	  orientationChoice.setEnabled(false);
	}
      } else {
	nameField.setText("");
	orientationLabel.setEnabled(false);
	orientationChoice.setEnabled(false);
      }
    }

    @Override
    public void applyOption() {
      if (curNode != null) {
	String name = nameField.getText();
	curNode.setName(name);
	if (curNode instanceof RegularPolygonShape) {
	  RegularPolygonShape regNode = (RegularPolygonShape) curNode;
	  regNode.setDirection(orientationChoice.getSelectedIndex()
	      + ShapeConstants.NORTH);
	} else if (curNode instanceof RectangularShape) {
	  RectangularShape rectNode = (RectangularShape) curNode;
	  rectNode.setDirection(orientationChoice.getSelectedIndex()
	      + ShapeConstants.NORTH);
	}

	canvas.repaintShape(curNode);
      }
    }

    protected JTextField nameField;

    protected JLabel orientationLabel;

    protected JComboBox orientationChoice;

  }

  class GraphEdgePanel extends OptionPanel {

    public GraphEdgePanel(String name) {
      super(name);
      // ResourceManager resourceManager = view.getResourceManager();
      // JLabel authorLabel = new
      // JLabel(resourceManager.getResourceString("Author.text") + " ");
      // authorField = new JTextField();

      mainPanel.setLayout(new DialogLayout());

      for (int i = 0; i < 5; i++) {
	labelFields[i] = new JTextField();
	addRow(new JLabel(labelNames[i]), labelFields[i]);
      }

      JLabel layoutLabel = new JLabel("Label layout: ");
      layoutChoice = new JComboBox();
      layoutChoice.addItem("Default");
      layoutChoice.addItem("Horizontal");
      layoutChoice.addItem("Parallel");

      addRow(layoutLabel, layoutChoice);

      setPreferredSize(new Dimension(300, 300));
      // setOption();
    }

    @Override
    public void setOption() {
      if (curEdge != null) {
	for (int i = 0; i < 5; i++) {
	  String label = curEdge.getLabel(i);
	  labelFields[i].setText(label);
	}
	layoutChoice.setSelectedIndex(curEdge.getEdgeLayoutPolicy());
      } else {
	for (int i = 0; i < 5; i++) {
	  labelFields[i].setText("");
	}
	layoutChoice.setSelectedIndex(0);
      }
    }

    @Override
    public void applyOption() {
      if (curEdge != null) {
	for (int i = 0; i < 5; i++) {
	  String label = labelFields[i].getText();
	  curEdge.setLabel(i, label);
	}

	curEdge.setEdgeLayoutPolicy(layoutChoice.getSelectedIndex());

	canvas.repaintShape(curEdge);
      }
    }

    protected String[] labelNames = { "Edge label: ", "Tail label: ",
	"Tail label 2: ", "Head label: ", "Head label 2: ", };

    protected JTextField[] labelFields = new JTextField[5];

    protected JComboBox layoutChoice;

  }

}
